package filter

import (
	"SortSet/server/resp"
	"fmt"
	"net/http"
)

func Err(h func(writer http.ResponseWriter, request *http.Request) error) func(writer http.ResponseWriter, request *http.Request) {
	return func(writer http.ResponseWriter, request *http.Request) {
		defer func() {
			if r := recover(); r != nil {
				fmt.Println("err:", r)
				resp.NewRespAndWriteDirect(
					400,
					"系统异常",
					nil,
					writer)
				return
			}
		}()
		err := h(writer, request)
		if err != nil {
			fmt.Println("err:", err)
			resp.NewRespAndWriteDirect(
				400,
				err.Error(),
				nil,
				writer)
			return
		}
	}
}
